package com.leetcode.二分查找;

public class lc_35 {
    public int searchInsert(int[] nums, int target) {
        return search3(nums, target);
    }
    //左开右开区间
    private int search1(int[] nums, int target){
        int left = -1;
        int right = nums.length;
        while(left + 1 < right){
            int mid = left + (right - left) / 2;
            if(nums[mid] > target){
                right = mid;
            } else if(nums[mid] < target){
                left = mid;
            } else {
                return mid;
            }
        }
        return right;
    }
    //左闭右闭区间
    private int search2(int[] nums, int target){
        int left = 0;
        int right = nums.length - 1;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if(nums[mid] > target){
                right = mid - 1;
            } else if(nums[mid] < target){
                left = mid + 1;
            } else{
                return mid;
            }
        }
        return left;
    }

    //左闭右开
    private int search3(int[] nums, int target){
        int left = 0;
        int right = nums.length;
        while(left < right){
            int mid = left + (right - left) / 2;
            if(nums[mid] > target){
                right = mid;
            } else if(nums[mid] < target){
                left = mid + 1;
            } else{
                return mid;
            }
        }
        return right;
    }
}
